<?php

namespace app\admin\controller;

use app\admin\common\Base;
use app\admin\model\News as NewsModel;
use app\admin\validate\News as NewsValidate;

class News extends Base {
    public function index($keyword = '',$page = 1,$limit = 10){
        if (is_post()){
            if (!empty($keyword)){
                $where[] = ['title','like','%' . $keyword . '%'];
            }
            $where[] = ['is_del','=',0];
            $data = $this->getListData('news',$where,'sort','asc',$page,$limit);

            foreach ($data['list'] as $k => $v){
                $data['list'][$k]['time'] = date('Y-m-d',$v['time']);
            }
            return json(['code'=>'suc','msg'=>'数据请求成功','count'=>$data['count'],'data'=>$data['list'],'current_page'=>$page]);
        }

        return $this->fetch();
    }

    public function add(){
        if (is_post()){
            //实例化模型
            $model = new NewsModel();
            //接收数据
            $data = input('post.');
            //执行数据库操作
            $result = $model->add($data);

            return json(['code'=>$result['code'],'msg'=>$result['msg']]);
        }

        return $this->fetch();
    }

    public function edit($id){
        if (is_post()){
            //实例化模型
            $model = new NewsModel();
            //接收数据
            $data = input('post.');
            //执行数据库操作
            $result = $model->edit($id,$data);

            return json(['code'=>$result['code'],'msg'=>$result['msg']]);
        }

        $info = db('news')
            ->where('id',$id)
            ->find();
        $info['time'] = date('Y-m-d',$info['time']);
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 复制
     * @param $data string 要复制的记录内容,json字符串
     * @return \think\response\Json
     */
    public function copy($data){
        if (is_post()){
            $data = json_decode($data,true);
            $ids = [];
            foreach ($data as $k => $v){
                $data[$k]['time'] = strtotime($v['time']) < 0 ? time() : strtotime($v['time']);
                $data[$k]['create_time'] = time();
                $data[$k]['update_time'] = time();
                array_push($ids,intval($v['id']));

                if (!empty($v['seo_link'])){
                    $data[$k]['seo_link'] = $v['seo_link'] . mt_rand(0,99);
                }

                unset($data[$k]['id']);
            }
            $ids = implode(',',$ids);
            $result = db('news')->insertAll($data);
            if ($result){
                insert_system_log('复制新闻：' . $ids);
                return json(['code' => 'suc', 'msg' => '复制成功']);
            }else{
                return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
            }
        }
    }

    public function del($id){
        if (is_post()){
            if ($this->delRecord('news',$id,'删除新闻：')){
                return json(['code' => 'suc','msg' => '已删除']);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    public function changeStatus($id,$type,$field){
        if (is_post()){
            switch ($field){
                case 1:
                    $log_action = $type==1 ? '显示' : '隐藏';
                    $action_str = $type==1 ? '已显示' : '已隐藏';
                    break;
                case 2:
                    $log_action = $type==1 ? '推荐' : '取消推荐';
                    $action_str = $type==1 ? '已推荐' : '取消推荐';
                    break;
                default:
                    $log_action = $type==1 ? '显示' : '隐藏';
                    $action_str = $type==1 ? '已显示' : '已隐藏';
            }
            if ($this->changeRecordStatus('news',$id,$field,$type)){
                insert_system_log($log_action . '新闻：' . $id);
                return json(['code'=>'suc','msg'=>$action_str . '新闻']);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    public function upload(){
        $file = request()->file('file');
        $info = $this->uploadsImg($file,'uploads/news/');
        if ($info !== false){
            return json(['code' => 'suc','msg' => '图片上传成功','url' => $info['path']]);
        }else{
            return json(['code' => 'err','msg' => '图片上传失败，请重试']);
        }
    }
}